package com.lanou.musicbox.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;

import com.lanou.musicbox.R;


/**
 * Created by 赵宝龙 on 15/11/10.
 * 进入页
 */
public class CoverActivity extends AppCompatActivity {
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    private boolean flag = true;
    private Handler handler;
    private View view;// 定义全局变量view
    private AlphaAnimation alphaAnimation;// 定义淡入淡出的动画效果

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 添加布局文件
        view = View.inflate(CoverActivity.this, R.layout.cover, null);
        // 获取布局
        setContentView(view);
        // 定义一个用来设置动画效果的方法
        initData();// 定义一个隐藏显示的方法
        Data();// 定义一个书写字幕闪耀的方法
    }

    private void Data() {
        preferences = getSharedPreferences("phone", Context.MODE_PRIVATE);
        if (flag) {
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    if (preferences.getBoolean("firstStart", true)) {
                        editor = preferences.edit();
                        editor.putBoolean("firstStart", false);
                        editor.commit();
                        Intent intent = new Intent(CoverActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        Intent intent = new Intent(CoverActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
            }, 3000);
            flag = false;
        }
    }

    private void initData() {
        alphaAnimation = new AlphaAnimation(0.1f,1.0f);
        alphaAnimation.setDuration(3000);

        view.setAnimation(alphaAnimation);

        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //               redirecTo();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
}
